#include <am.h>
#include <riscv/riscv.h>

void __am_uart_init() {
  // outb(0x10000000L + 2, 0b1100110); // reset t/r fifo
  // outb(0x10000000L + 2, 0b1100000);
  outb(0x10000000L + 3, 0b10000011); // set line control register to access Divisor Latch

  outb(0x10000000L + 1, 0); // set MSB of devisor latch
  outb(0x10000000L, 10); // set LSB of devisor latch

  outb(0x10000000L + 3, 0b00000011); // set line control register to transmit data. 8N1
}

void __am_uart_rx(AM_UART_RX_T *buf) {
  char ready = inb(0x10000000L + 5) & 0b00000001;
  buf->data = ready == 1 ? inb(0x10000000L) : 0xff;
}
